<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>在线考试</title>
    <script src="http://47.106.66.89:8080/js/vue.min.js"></script>
    <script src="http://47.106.66.89:8080/js/axios.min.js"></script>
    <link href="http://47.106.66.89:8080/js/ele-2.15.7/theme-chalk/index.css" rel="stylesheet">
    <script src="http://47.106.66.89:8080/js/ele-2.15.7/index.js"></script>
</head>
<body>
<div id="app">
    <header>
        <div class="c900 flex">
            <a href="./"><h1 style="color: #fdfdfd">在线考试</h1></a>
            <span class="flex">
                <a href="#"><el-avatar src="https://img1.baidu.com/it/u=2068393135,2936134418&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"></el-avatar></a>
                <a href="register.html">注册</a>
                <a href="login.html">登录</a>
            </span>
        </div>
    </header>
    <main class="c900">
        <h1 style="width: 40%;margin: 20px auto">登录平台</h1>
        <form class="grid2">
            <label for="">账号:</label><input type="text" v-model="user.name">
            <span class="err" v-html="msgs.name"></span>
            <label for="">密码:</label><input type="password" v-model="user.pwd">
            <span class="err" v-html="msgs.pwd"></span>
            <div style="grid-column: 2; text-align: end">
                <a href="register.html" style="margin-right: 20px">注册</a>
                <button style="padding: 5px 20px" @click.prevent = "log">登录</button>
            </div>
        </form>
    </main>
</div>
<script>
    var v = new Vue({
        el : "#app",
        data : {
            user:{name:"",pwd:""},
            msgs:{name:"",pwd:""}
        },
        created() {
            //初始化数据模型，没有渲染到画面
        },
        mounted(){
            //完成第一次渲染到页面
            if (location.search=="?1"){
                this.$alert("请先登录系统");
            }
        },
        updated(){
            //第N次渲染，没次数据模型改变，都会触发该钩子函数
        },
        methods : {
            log(){
                axios.post(
                    "log",
                    this.user
                ).then(resp=>{
                    this.msgs= {name:"",pwd:""};
                    // console.log(resp.data)
                    if(resp.data.code==1){
                        this.$message(resp.data.message);
                        if (resp.data.data == null){
                            location.href = "index.html";
                        }else {
                            location.href = resp.data.data;
                        }

                        return;
                    }else {
                        this.$alert(resp.data.message)
                    }
                    resp.data.data.forEach(err=>{
                        if(this.msgs[err.field]){
                            this.msgs[err.field] += "<br>";
                        }
                        this.msgs[err.field] += err.defaultMessage ;
                    });
                    console.log(this.msgs)
                });
            }
        }
    })
</script>

<style>
    *{
        color:#222
    }
    .c900>form{
        width: 40%;
        margin: auto;
        padding: 30px;
        border: 1px green solid;
        border-radius: 10px;
        background-color: lightblue;
    }
    .c900>form label,.c900>form input{
        text-align: left;
        box-sizing: border-box;
        height: 35px;
        display: flex;
        align-items: center;
        outline: none;
        padding: 10px;
    }

    html,body,#app{
        height: 100%;
    }
    body{
        margin: 0px;
        padding: 0px;
        background-size: cover;
        background-image: url("https://img0.baidu.com/it/u=668574498,1720392676&fm=253&fmt=auto&app=138&f=JPEG?w=658&h=295");
    }
    a{
        text-decoration: none;
        color: #666
    }
    img{
        width: 100%;
        object-fit: cover;
        border-radius: 5px;
    }
    .c900{
        width: 900px;
        margin-inline: auto;
    }
    .flex{
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
    }
    .grid2{
        display: grid;
        grid-gap: 10px;
        grid-template-columns: auto auto;
    }
    .grid3{
        display: grid;
        grid-template-columns: auto auto auto;
    }
    .grid4{
        display: grid;
        grid-template-columns: auto auto auto auto;
    }
    #app>header{
        background-color: #0f9ae0;
        background-size: cover;
        position: sticky;
        top: 0px;
        z-index: 1000;
        height: 60px;
    }
    #app>header h1{
        margin: 10px;
    }
    #app>header span>a{
        margin-left: 20px;
        color: #fff;
    }
    #app>main{
        background-color: #eee8;
        min-height: calc(100% - 100px);
        padding: 20px;
    }
    .err{
        grid-column: 2;
        color: red
    }
</style>
</body>
</html>